﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"workerManager.h"
#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
void WorkerManager::exitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	return;
}
WorkerManager::WorkerManager()
{
	fstream ifs;
	ifs.open("FILENAME", ios::in);
	//1.文件不存在
	if (!ifs.is_open())
	{
		this->_size = 0;
		this->m_EmpArray = NULL;
		this->_Fileisempty = true;
		ifs.close();
		return;
	}
	//文件存在并且文件为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		this->m_EmpArray = NULL;
		this->_size = 0;
		this->_Fileisempty = true;
		ifs.close();
		return;
	}
	//文件存在并且文件不为空
	int sum = Getsum();
	this->_size = sum;//更新职工个数
	this->m_EmpArray = new worker* [this->_size];
	init_emp();
}
WorkerManager::~WorkerManager()
{
	if (this->m_EmpArray != NULL)
	{
		delete[] this->m_EmpArray;
	}
}
//添加职工
void WorkerManager::add_emp()
{
	cout << "请输入要添加职工的数量：" << endl;
	int addsum;
	cin >> addsum;
	if (addsum > 0)
	{
		//计算新空间的大小
		int newSize = this->_size + addsum;
		//开辟新空间//指针数组，每个指针指向worker类型的结构体
		worker** newspace = new worker* [newSize];
		//将旧空间的内容挪动到新空间
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < this->_size; i++)
			{
				newspace[i] = this->m_EmpArray[i];
			}
		}
		//输入新的数据
		for (int i = 0; i < addsum; i++)
		{
			int id = 0;
			string name;
			int dept_id = 0;
			cout << "请输入第" << i + 1 << "个新员工编号：" << endl;
			cin >> id;
			cout << "请输入第" << i + 1 << "个新员工姓名：" << endl;
			cin >> name;
			int position = 0;
			cout << "请选择该员工的职位：" << endl;
			cout << "1. 普通员工" << endl;
			cout << "2. 经理" << endl;
			cout << "3. 老板" << endl;
			cin >> position;
			//创建结构体指针
			worker* worker = NULL;
			switch (position)
			{
			case 1://普通员工
				worker = new employee(id, name, position);
				break;
			case 2://经理
				worker = new manager(id, name, position);
				break;
			case 3://老板
				worker = new boss(id, name, position);
				break;
			default:
				break;
			}
			//_size只是记录原来数组中有几个元素
			newspace[this->_size + i] = worker;
		}
		//释放原来指针数组的空间
		delete[] this->m_EmpArray;
		//更改新空间的指向
		this->m_EmpArray = newspace;
		//更新元素个数
		this->_size = newSize;
		//提示添加信息
		cout << "成功添加了" << addsum << "个职工!" << endl;
		save();
		this->_Fileisempty = false;
	}
	else {
		cout << "输入错误" << endl;
	}
	system("pause");
	system("cls");
}


//文件的保存
void WorkerManager::save()
{
	ofstream ofa;//创建流对象,ofstream 本身就像类，类中包括了各种对文件的行为。
	ofa.open("FILENAME", ios::out);//open()函数就是，ofstream类中的行为
	for (int i = 0; i < this->_size; i++)
	{
		ofa << this->m_EmpArray[i]->_Id << " "
			<< this->m_EmpArray[i]->_name << " "
			<< this->m_EmpArray[i]->_dept_id << endl;
	}
	ofa.close();

}
//获取职工的人数
int WorkerManager::Getsum()
{
	fstream ifs;
	ifs.open("FILENAME", ios::in);
	int id = 0;
	string name;
	int dept_id = 0;
	int sum = 0;
	while (ifs >> id && ifs >> name && ifs >> dept_id)
	{
		sum++;
	}
	ifs.close();
	return sum;
}
void WorkerManager::init_emp()
{
	fstream ifs;
	ifs.open("FILENAME", ios::in);
	int id = 0;
	string name;
	int dept_id = 0;
	int index = 0;
	worker* worker = NULL;
	while (ifs >> id && ifs >> name && ifs >> dept_id)
	{
		if (dept_id == 1)
		{
			worker = new employee(id, name, dept_id);
		}
		else if (dept_id == 2)
		{
			worker = new manager(id, name, dept_id);
		}
		else if (dept_id == 3)
		{
			worker = new boss(id, name, dept_id);
		}
		this->m_EmpArray[index] = worker;
		index++;
	}

}

void WorkerManager::Show_emp()
{
	if (this->_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else {
		for (int i = 0; i < this->_size; i++)
		{
			this->m_EmpArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}
//判断该职员是否存在
int WorkerManager::Is_exist(int id)
{
	int index = -1;
	for (int i = 0; i < this->_size; i++)
	{
		if (this->m_EmpArray[i]->_Id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}
//删除职工
void WorkerManager::Dele_emp()
{
	if (this->_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else {
		cout << "请输入要删除员工的id：" << endl;
		int id = 0;
		cin >> id;
		int put = Is_exist(id);
		if (put != -1)
		{
			int tail = put;
			while (tail <this->_size-1)
			{
				this->m_EmpArray[tail] = this->m_EmpArray[tail+1];
				tail++;
			}
			this->_size--;
			this->save();//同步到文件中
			cout << "删除成功" << endl;
			if (this->_size == 0)
			{
				this->_Fileisempty = true;
			}
		}
		else {
			cout << "删除失败,未找到该员工" << endl;
		}
	}
	system("pause");
	system("cls");
}
//修改职员信息
void WorkerManager::Mod_emp()
{
	if (this->_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else
	{
		cout << "请输入要修改职员的id：" << endl;
		int id = 0;
		cin >> id;
		//判断该职员是否存在
		int index = Is_exist(id);
		if (index != -1)
		{
			int id = 0;
			string name;
			int dept_id = 0;
			cout << "请输入新编号：" << endl;
			cin >> id;
			cout << "请输入新姓名：" << endl;
			cin >> name;
			int position = 0;
			cout << "请选择该员工的职位：" << endl;
			cout << "1. 普通员工" << endl;
			cout << "2. 经理" << endl;
			cout << "3. 老板" << endl;
			cin >> position;
			//创建结构体指针
			worker* worker = NULL;
			switch (position)
			{
			case 1://普通员工
				worker = new employee(id, name, position);
				break;
			case 2://经理
				worker = new manager(id, name, position);
				break;
			case 3://老板
				worker = new boss(id, name, position);
				break;
			default:
				break;
			}
			//_size只是记录原来数组中有几个元素
			this->m_EmpArray[index] = worker;
			cout<<"修改成功！" << endl;
			//保存到文件
			this->save();
		}
		else {
			cout << "修改失败,未找到该员工" << endl;
		}
	}

	system("pause");
	system("cls");
}
//查早职员
void WorkerManager::Find_emp()
{
	if (this->_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
		system("pause");
		system("cls");
	}
	int buff = 0;
	cout << "请选择查找方式：" << endl;
	cout << " 1. 按工号查找" << endl;
	cout << " 2. 按姓名查找" << endl;
	cin >> buff;
	if (buff == 1)
	{
		//按工号查找
		cout << "请输入职员的编号：" << endl;
		int id = 0;
		cin >> id;
		//判断该职员是否存在
		int index = this->Is_exist(id);
		if (index != -1)
		{
			//职员存在
			cout << "找到了，职工编号为" << this->m_EmpArray[index]->_Id << " 号，的信息如下：" << endl;
			this->m_EmpArray[index]->showInfo();

		}
		else {
			//职员不存在
			cout << "查找失败,查无此人" << endl;
		}
	}
	else if(buff == 2)
		{
		//按姓名查找
		cout << "请输入要找的员工的姓名：" << endl;
		string name;
		cin >> name;
		int flag = 0;
		for (int i = 0; i < this->_size; i++)
		{
			if (this->m_EmpArray[i]->_name == name)
			{
				cout << "找到了，职工编号为" << this->m_EmpArray[i]->_Id << " 号，的信息如下：" << endl;
				this->m_EmpArray[i]->showInfo();
				flag = 1;
			}
		}
		if (flag == 0)
		{
			cout << "查找失败,查无此人" << endl;
		}
	}
	else {
		cout << "输入选项错误!" << endl;
	}

	system("pause");
	system("cls");
}

//职员排序
void WorkerManager::Sort_emp()
{
	if (this->_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
		system("pause");
		system("cls");
	}
	else {
		cout << "请选择排序方式：" << endl;
		cout << "1. 按职工号进行升序" << endl;
		cout << "2. 按职工号进行降序" << endl;
		int select = 0;
		cin >> select;
		for (int i = 0; i < this->_size; i++)
		{
			int minOrmax = i;
			if (select == 1)
			{
				//排升序
				for (int j = i + 1; j < this->_size; j++)
				{
					if (this->m_EmpArray[j]->_Id < this->m_EmpArray[minOrmax]->_Id)
					{
						minOrmax = j;
					}
				}
			}
			else {
				//排降序
				for (int j = i + 1; j < this->_size; j++)
				{
					if (this->m_EmpArray[j]->_Id > this->m_EmpArray[minOrmax]->_Id)
					{
						minOrmax = j;
					}
				}
			}
			if (i != minOrmax)
			{
				worker* tem = this->m_EmpArray[i];
				this->m_EmpArray[i] = this->m_EmpArray[minOrmax];
				this->m_EmpArray[minOrmax] = tem;	
			}
		}
		//排序成功结果为
		cout << "排序成功，排序后结果为：" << endl;
		this->Show_emp();
		//保存到文件
		this->save();
	}
}

void WorkerManager::Clean_file()
{
	cout << "确认清空？" << endl;
	cout << "1.确认" << endl;
	cout << "0.返回" << endl;
	int select = -1;
	cin >> select;
	if (select == 1)
	{
		//清空实现
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < this->_size; i++)
			{
				if (this->m_EmpArray[i] != NULL)
				{
					delete this->m_EmpArray[i];
				}
			}
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->_size = 0;
			this->_Fileisempty = true;	
		}
		cout << "清空成功！" << endl;

	}
	system("pause");
	system("cls");
}
//void test()
//{
//	worker* worker = NULL;
//	worker = new employee(1, "张三", 1);
//	worker->showInfo();
//	delete worker;
//
//	worker = new manager(2, "李四", 2);
//	worker->showInfo();
//	delete worker;
//
//	worker = new boss(3, "王五", 3);
//	worker->showInfo();
//	delete worker;
//}
int main()
{
	/*test();*/
	WorkerManager wm;
	int chice = 0;
	while (true)
	{
		wm.Show_Menu();
		cout << "请输入你的选择：" << endl;
		cin >> chice;
		switch (chice)
		{
		case 0 ://退出管理系统
			wm.exitSystem();
			break;
		case 1://增加职工信息
			wm.add_emp();
			break;
		case 2://显示职工信息
			wm.Show_emp();
			break;
		case 3://删除离职职工
			wm.Dele_emp();
			break;
		case 4://修改职工信息
			wm.Mod_emp();
			break;
		case 5://查找职工信息
			wm.Find_emp();
			break;
		case 6://按照编号排序
			wm.Sort_emp();
			break;
		case 7://清空所有文档
			wm.Clean_file();
			break;
		default:
			cout << "输入错误!" << endl;
			system("cls");
			break;
		}
		
	}
	system("pause");

	return 0;
}